<template>
  <el-dialog :title="title" v-model="open" width="500px" append-to-body @closed="cancel">
    <el-form ref="deviceParamRef" :model="form" label-width="120px">
      <template v-for="(item,index) in paramType" :key="index">
        <el-form-item v-if="!(deviceType === 5 && item.value === '6012')"
            :label="item.label" :prop="item.value" :rules="[
        {
          required: true,
          message: item.label+'不能为空',
          trigger: 'blur',
        },

      ]">
          <el-input v-model="form[item.value]" width="" :placeholder="'请输'+item.label">
            <template #append>{{ item.unit }}</template>
          </el-input>
        </el-form-item>
      </template>
      <template v-for="(item,index) in paramType" :key="index">
        <el-form-item v-if="deviceType === 5 && item.value === '6012'"
                      :label="item.label" :prop="item.value" :rules="[
        {
          required: true,
          message: item.label+'不能为空',
          trigger: 'change',
        },

      ]">
          <el-select v-model="form[item.value]" style="width: 100%" :placeholder="'请选择'+item.label">
            <el-option label="顺时针" value="1" />
            <el-option label="逆时针" value="0" />
          </el-select>
        </el-form-item>
      </template>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup name="DeviceParam">
import { getDeviceParam,saveDeviceParam } from "@/api/coal/device";

const props = defineProps({
  propDeviceId: {
    type: Number,
    default: null,
  },
  propDeviceType: {
    type: Number,
    default: null,
  },
  propOpen:{
    type:Boolean,
    default: false,
  },
  propParamType:{
    type:Array,
    default: [],
  }
})
const emit = defineEmits(['closeParam']);

const deviceId = ref(props.propDeviceId)
const deviceType = ref(props.propDeviceType)
const open = ref(props.propOpen)
const paramType = ref(props.propParamType)

const { proxy } = getCurrentInstance();
const router = useRouter();

const title = ref('设备参数设置')

const data = reactive({
  form: {},
});

const { form } = toRefs(data);

/** 提交按钮 */
function submitForm() {
  proxy.$refs["deviceParamRef"].validate(valid => {
    if (valid) {
      form.value['deviceId'] = deviceId.value
      form.value['deviceType'] = deviceType.value
      saveDeviceParam(form.value).then(resp=>{
        proxy.$modal.msgSuccess("修改成功");
        cancel()
      })
    }
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  emit('closeParam');
  //reset();
}
onMounted(() => {
  let dictValues = paramType.value.map(item => item.value )

  if (dictValues.length === 0){
    cancel()
    proxy.$modal.msg("该设备无需配置参数");
    return
  }
  getDeviceParam(deviceId.value,dictValues).then(resp => {
    if (resp.data){
      let paramData = resp.data
      Object.keys(paramData).map(key=>{
        form.value[key] = paramData[key]
      })
    }
  })
})

</script>
